//
//  CorpusViewController.m
//  项目PianKe
//
//  Created by 关雷 on 14-12-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CorpusViewController.h"
#import "AFNetWoringYu.h"
#import "UIImageView+WebCache.h"
#import "CorpusModel.h"
#import "CorpusUserModel.h"
#import "ReadCorpusCollectionViewCell.h"
#import "UIScrollView+MJRefresh.h"
#import "WebColor.h"
#import "CorpusDetailViewController.h"
@interface CorpusViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, assign) NSInteger start;
@end

@implementation CorpusViewController
- (void)dealloc
{
    [_collectArray release];
    [_collecttion release];
    [_corpusUserArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.frame = CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height - 45);
        
        self.collectArray = [NSMutableArray array];
        self.corpusUserArray = [NSMutableArray array];
        self.view.backgroundColor = [WebColor gainsboro];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startConnectWithUrl:@"http://api.pianke.me/read/album" body:@"client=1&limit=6&start=0"];
    [self createCollectionView];
    [self headerRefreshing];
    [self footerLoading];
    // Do any additional setup after loading the view.
}
//网络请求,数据解析
- (void)startConnectWithUrl:(NSString *)url body:(NSString *)body
{
    [AFNetWoringYu netWorkWithURL:url body:body resultBlock:^(id dic) {
        NSDictionary * adic = [dic objectForKey:@"data"];
        NSArray * array = [adic objectForKey:@"list"];
        for (NSDictionary * dics in array) {
            CorpusModel * model = [[CorpusModel alloc] init];
            [model setValuesForKeysWithDictionary:dics];
            CorpusUserModel * aModel = [[CorpusUserModel alloc] init];
            [aModel setValuesForKeysWithDictionary:model.userinfo];
            [self.collectArray addObject:model];
            [self.corpusUserArray addObject:aModel];
            [model release];
            [aModel release];
        }
        [_collecttion reloadData];
    }];
    
}
//创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc] init];
    flow.minimumLineSpacing = 5;
    flow.minimumInteritemSpacing = 5.0f;
    flow.itemSize = CGSizeMake(self.view.frame.size.width / 2 - 10, self.view.frame.size.height / 2);
    flow.sectionInset = UIEdgeInsetsMake(10, 5, 100, 5);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collecttion = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    [_collecttion
     registerClass:[ReadCorpusCollectionViewCell class] forCellWithReuseIdentifier:@"cellCorpus"];
    _collecttion.delegate = self;
    _collecttion.dataSource = self;
    _collecttion.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collecttion];
    [_collecttion release];
    [flow release];
    
}
//cell的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_collectArray count];
}
//为cell赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReadCorpusCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellCorpus" forIndexPath:indexPath];
    CorpusModel * model = [_collectArray objectAtIndex:indexPath.row];
    CorpusUserModel * uModel = [_corpusUserArray objectAtIndex:indexPath.row];
   
    [cell.igmV setImageWithURL:[NSURL URLWithString:model.coverimg]];
    NSString * start = @"";
    if (model.finish == 1) {
        start = @"连载中";
    } else {
        start = @"已完结";

    }
    cell.upLable.text = start;
    cell.downLableName.text = uModel.uname;
    return cell;
}
//下拉刷新
- (void)headerRefreshing
{
    [self.collecttion addHeaderWithCallback:^{
        
        [AFNetWoringYu netWorkWithURL:@"http://api.pianke.me/read/album" body:@"client=1&limit=6&start=0" resultBlock:^(id dic) {
            [self.collectArray removeAllObjects];
            NSDictionary * adic = [dic objectForKey:@"data"];
            NSArray * array = [adic objectForKey:@"list"];
            for (NSDictionary * dics in array) {
                CorpusModel * model = [[CorpusModel alloc] init];
                [model setValuesForKeysWithDictionary:dics];
                CorpusUserModel * aModel = [[CorpusUserModel alloc] init];
                [aModel setValuesForKeysWithDictionary:model.userinfo];
                [self.collectArray addObject:model];
                [self.corpusUserArray addObject:aModel];
                [model release];
                [aModel release];
            }
            [_collecttion reloadData];
            [self.collecttion headerEndRefreshing];
        }];
    }];
}
//上拉加载
- (void)footerLoading
{
    self.start = 6;
    [self.collecttion addFooterWithCallback:^{
        if (_start < 18) {
            NSString * str = [NSString stringWithFormat:@"%@%ld", @"client=1&limit=6&start=", _start];
            [self startConnectWithUrl:@"http://api.pianke.me/read/album" body:str];
            [self.collecttion footerEndRefreshing];
            _start += 6;
        }
        }];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    CorpusModel * model = [_collectArray objectAtIndex:indexPath.row];
    CorpusDetailViewController * corD = [[CorpusDetailViewController alloc] init];
    corD.albumid = model.albumid;
    [self.navigationController pushViewController:corD animated:YES];
    [corD release];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
